package com.midea.ai.air.ui.more;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.common.ActionBarBackFragment;
import com.midea.ai.air.common.BaseActivity;
import com.midea.ai.air.common.Constant;
import com.midea.ai.air.view.NumberPickerDialog;
import com.midea.ai.air.view.wheelview.ArrayWheelAdapter;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelView;

/**
 * Created by hww on 2016/3/29.
 * 更多-设置-定位开关
 */
public class MoreSettingLocationOnOff extends BaseActivity implements View.OnClickListener {

    public static final String[] sSelectMinutes = new String[] {
            "1分钟","2分钟","3分钟","4分钟", "5分钟","6分钟","7分钟","8分钟", "9分钟","10分钟","15分钟",
            "20分钟", "25分钟","30分钟","35分钟","40分钟", "45分钟","50分钟","55分钟","60分钟"};
    public static String POSITION = "AIR_EXPERT_POSITION";
    public static String TEMP = "AIR_EXPERT_TEMP";

    ActionBarBackFragment mActionBar;

    private ImageView bt_location, bt_environment;
    private RelativeLayout mLayoutOn, mLayoutOff;
    private TextView mTvOn, mTvOff;
    private LinearLayout linearLayout;

    private boolean isShow_location, isShow_environment;
    private int mCurrentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moresettinglocationonoff);

        init();
    }

    private void init() {
        initActionBar();

        bt_location = (ImageView) findViewById(R.id.more_setting_location_on_off);
        bt_environment = (ImageView) findViewById(R.id.more_setting_location_environment);
        linearLayout = (LinearLayout) findViewById(R.id.more_setting_location_linearlayout);

        mLayoutOn = (RelativeLayout) findViewById(R.id.layout_switch_on);
        mLayoutOn.setOnClickListener(this);

        mLayoutOff = (RelativeLayout) findViewById(R.id.layout_switch_off);
        mLayoutOff.setOnClickListener(this);

        mTvOn = (TextView) findViewById(R.id.switch_on_time);
        mTvOff = (TextView) findViewById(R.id.switch_off_time);

        bt_location.setOnClickListener(this);
        bt_environment.setOnClickListener(this);
    }

    private void initActionBar() {
        FrameLayout mBarLayout = (FrameLayout) findViewById(R.id.bar_parent);
        ViewGroup.LayoutParams LP = mBarLayout.getLayoutParams();
        LP.height = Constant.getTopMargin() + getResources().getDimensionPixelSize(R.dimen.height_44dp);
        mBarLayout.setLayoutParams(LP);
        ImageView rightBtn;
        RadioButton leftBtn;
        TextView tvTitle;

        mActionBar = (ActionBarBackFragment) getSupportFragmentManager().findFragmentById(R.id.bar_fragment);
        mActionBar.getBarFullLayout().setBackgroundColor(getResources().getColor(R.color.common_blue));

        rightBtn = mActionBar.getIvBarRight();
        rightBtn.setVisibility(View.VISIBLE);
        rightBtn.setImageResource(R.drawable.more_setting_location_right);
        rightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MoreSettingLocationOnOff.this, MoreSettingLocationOnOffLogging.class));
            }
        });

        leftBtn =  mActionBar.getBackButton();
        leftBtn.setText(getString(R.string.nav_bar_more));
        leftBtn.setTextColor(getResources().getColor(R.color.common_white));
        mActionBar.setBackLeftIcon(R.drawable.air_action_bar_left_white);

        tvTitle = mActionBar.getTvTitle();
        tvTitle.setText(getString(R.string.more_setting_location));
        tvTitle.setTextColor(getResources().getColor(R.color.common_white));

        mActionBar.getBotLine().setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.more_setting_location_on_off:
                //定位开关按钮
                if (isShow_location) {
                    isShow_location = false;
                    bt_location.setImageResource(R.drawable.more_setting_location_bt_on);
                    linearLayout.setVisibility(View.VISIBLE);
                } else {
                    isShow_location = true;
                    bt_location.setImageResource(R.drawable.more_setting_location_bt_off);
                    linearLayout.setVisibility(View.GONE);
                }

                break;
            case R.id.more_setting_location_environment:
                //环境开关按钮
                if (isShow_environment) {
                    isShow_environment = false;
                    bt_environment.setImageResource(R.drawable.more_setting_location_bt_on);
                } else {
                    isShow_environment = true;
                    bt_environment.setImageResource(R.drawable.more_setting_location_bt_off);
                }
                break;

            case R.id.layout_switch_on:
                //提前开机
                mCurrentIndex = getCurrentIndex(mTvOn.getText().toString().trim());
                createNumberPicker(R.id.layout_switch_on);
                break;

            case R.id.layout_switch_off:
                //离家关机
                mCurrentIndex = getCurrentIndex(mTvOff.getText().toString().trim());
                createNumberPicker(R.id.layout_switch_off);
                break;

            default:
                break;
        }
    }

    private void createNumberPicker(final int resId) {
        NumberPickerDialog numPickerDlg = new NumberPickerDialog(this).builder().setCyclic(true).setAdapter(new ArrayWheelAdapter<>(sSelectMinutes)).addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                int index = wheel.getCurrentItem();
                mCurrentIndex = index;

                switch (resId) {
                    case R.id.layout_switch_on:
                        Log.i("ouyangdi", "提前开机 结果：" + sSelectMinutes[index]);
                        break;

                    case R.id.layout_switch_off:
                        Log.i("ouyangdi", "离家关机 结果：" + sSelectMinutes[index]);
                        break;
                }
            }
        }).setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                switch (resId) {
                    case R.id.layout_switch_on:
                        mTvOn.setText(sSelectMinutes[mCurrentIndex]);
                        break;

                    case R.id.layout_switch_off:
                        mTvOff.setText(sSelectMinutes[mCurrentIndex]);
                        break;
                }
            }
        });
        numPickerDlg.setCurrentItem(mCurrentIndex);
        numPickerDlg.show();
    }

    private int getCurrentIndex(String str) {
        int index = 0;
        for(int i=0; i<sSelectMinutes.length; i++) {
            if(sSelectMinutes[i].equals(str)) {
                index =  i;
                break;
            }
        }
        return index;
    }
}
